const express = require('express')
const router = express.Router()
const axios = require('axios')
const config = require('../utils/config')
const getAccessToken = require('../utils/getAccessToken')
const apis = require('../utils/api')
const common = require('../utils/common')

//获取全部用户信息
router.get('/', (req, res) => {
  ;(async () => {
    try {
      const accessToken = await getAccessToken()
      const result = await axios({
        method: 'GET',
        url: 'https://api.weixin.qq.com/cgi-bin/user/get',
        params: {
          access_token: accessToken,
        },
      })
      if (!result.data.errcode) {
        res.json({
          state: '200',
          mag: '获取用户信息成功',
          data: result.data,
        })
      } else {
        res.json({
          state: '0',
          mag: '获取信息失败',
          err: result.data,
        })
      }
    } catch (e) {
      res.json({
        state: '0',
        msg: '获取用户信息失败',
        err: e,
      })
    }
  })()
})
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var d = date.getDay()
var weekday = [
  '星期日',
  '星期一',
  '星期二',
  '星期三',
  '星期四',
  '星期五',
  '星期六',
]
var time =
  year.toString() +
  '年' +
  month.toString() +
  '月' +
  day.toString() +
  '日   ' +
  weekday[d]

//推送模板
router.get('/template', (req, res) => {
  ;(async () => {
    config.users.openid.city = '常州'
    config.users.openid.month = '12'
    config.users.openid.month = '15'
    try {
      const accessToken = await getAccessToken()
      const weather = await apis.getWeather()
      const lzmy = await apis.getLzmy()
      var datas = null
      const h = date.getHours()
      const f = date.getMinutes()
      const times = h + '' + f
      //   控制时间该发送什么模板
      //   早上提醒
      if (times == '730') {
        const zaoan = await apis.getzaoan()
        const xinzuo = await apis.getxinzuo()
        datas = {
          touser: config.users.openid2,
          template_id: config.wx.template_id1,
          data: {
            date: {
              value: time,
              color: common.randomHexColor(),
            },
            city: {
              value: weather.area,
              color: common.randomHexColor(),
            },
            weather: {
              value: weather.weather,
              color: common.randomHexColor(),
            },
            min_temperature: {
              value: weather.lowest,
              color: common.randomHexColor(),
            },
            max_temperature: {
              value: weather.highest,
              color: common.randomHexColor(),
            },
            pop: {
              value: weather.pop,
              color: common.randomHexColor(),
            },
            mineBirthDays: {
              value: common.getDaysToBirthday(
                config.users.month,
                config.users.day
              ),
              color: common.randomHexColor(),
            },
            zaoan: {
              value: zaoan.content,
              color: common.randomHexColor(),
            },
            supei: {
              value: xinzuo[2].content,
              color: common.randomHexColor(),
            },
            xinzuo: {
              value: xinzuo[8].content,
              color: common.randomHexColor(),
            },
            lizhi: {
              value: lzmy.saying + ' ——— ' + lzmy.source,
              color: common.randomHexColor(),
            },
          },
        }
        // 中午提醒
      } else if (times == '1130') {
        const tiangou = await apis.gettiangou()
        const getchigua = await apis.getchigua()
        datas = {
          touser: config.users.openid2,
          template_id: config.wx.template_id2,
          data: {
            date: {
              value: time,
              color: common.randomHexColor(),
            },
            eate: {
              value: '记得吃饭啊,工作在忙身体最重要',
              color: common.randomHexColor(),
            },
            city: {
              value: weather.area,
              color: common.randomHexColor(),
            },
            weather: {
              value: weather.weather,
              color: common.randomHexColor(),
            },
            tiangou: {
              value: tiangou.content,
              color: common.randomHexColor(),
            },
            firstchigua: {
              value: getchigua[0].hotword,
              color: common.randomHexColor(),
            },
            secondchigua: {
              value: getchigua[1].hotword,
              color: common.randomHexColor(),
            },
            thirdchigua: {
              value: getchigua[2].hotword,
              color: common.randomHexColor(),
            },
          },
        }
      } else if (times == '210') {
        const tiangou = await apis.gettiangou()
        const jitang = await apis.getjitang()
        datas = {
          touser: config.users.openid2,
          template_id: config.wx.template_id3,
          data: {
            date: {
              value: `${h}:${f <= 10 ? '0' + f : f}`,
              color: common.randomHexColor(),
            },
            sleep: {
              value: '不早啦赶紧睡吧！好梦',
              color: common.randomHexColor(),
            },
            tiangou: {
              value: `舔狗日记: ${tiangou.content}`,
              color: common.randomHexColor(),
            },
            jitang: {
              value: `干了这碗鸡汤: ${jitang.content}`,
              color: common.randomHexColor(),
            },
          },
        }
      }
      const result = await axios({
        method: 'POST',
        url:
          'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' +
          accessToken,
        data: datas,
      })
      console.log('查看模板消息返回结果', result.data)
      res.send('推送成功')
    } catch (err) {
      console.log(err)
    }
  })()
})

router.get('/send', (req, res) => {
  ;(async () => {
    config.users.openid.city = '苏州'
    config.users.openid.month = '9'
    config.users.openid.month = '7'
    try {
      const accessToken = await getAccessToken()
      const weather = await apis.getWeather()
      const lzmy = await apis.getLzmy()
      var datas = null
      const h = date.getHours()
      const f = date.getMinutes()
      const times = h + '' + f
      //   控制时间该发送什么模板
      if (times == '730') {
        const zaoan = await apis.getzaoan()
        const xinzuo = await apis.getxinzuo()
        datas = {
          touser: config.users.openid1,
          template_id: config.wx.template_id1,
          data: {
            date: {
              value: time,
              color: common.randomHexColor(),
            },
            city: {
              value: weather.area,
              color: common.randomHexColor(),
            },
            weather: {
              value: weather.weather,
              color: common.randomHexColor(),
            },
            min_temperature: {
              value: weather.lowest,
              color: common.randomHexColor(),
            },
            max_temperature: {
              value: weather.highest,
              color: common.randomHexColor(),
            },
            pop: {
              value: weather.pop,
              color: common.randomHexColor(),
            },
            mineBirthDays: {
              value: common.getDaysToBirthday(
                config.users.month,
                config.users.day
              ),
              color: common.randomHexColor(),
            },
            zaoan: {
              value: zaoan.content,
              color: common.randomHexColor(),
            },
            supei: {
              value: xinzuo[2].content,
              color: common.randomHexColor(),
            },
            xinzuo: {
              value: xinzuo[8].content,
              color: common.randomHexColor(),
            },
            lizhi: {
              value: lzmy.saying + ' ——— ' + lzmy.source,
              color: common.randomHexColor(),
            },
          },
        }
      } else if (times == '120') {
        const tiangou = await apis.gettiangou()
        const getchigua = await apis.getchigua()
        datas = {
          touser: config.users.openid1,
          template_id: config.wx.template_id2,
          data: {
            date: {
              value: time,
              color: common.randomHexColor(),
            },
            eate: {
              value: '记得吃饭啊,工作在忙身体最重要',
              color: common.randomHexColor(),
            },
            city: {
              value: weather.area,
              color: common.randomHexColor(),
            },
            weather: {
              value: weather.weather,
              color: common.randomHexColor(),
            },
            tiangou: {
              value: tiangou.content,
              color: common.randomHexColor(),
            },
            firstchigua: {
              value: getchigua[0].hotword,
              color: common.randomHexColor(),
            },
            secondchigua: {
              value: getchigua[1].hotword,
              color: common.randomHexColor(),
            },
            thirdchigua: {
              value: getchigua[2].hotword,
              color: common.randomHexColor(),
            },
          },
        }
      } else if (times == '210') {
        const tiangou = await apis.gettiangou()
        const jitang = await apis.getjitang()
        datas = {
          touser: config.users.openid1,
          template_id: config.wx.template_id3,
          data: {
            date: {
              value: `${h}:${f <= 10 ? '0' + f : f}`,
              color: common.randomHexColor(),
            },
            sleep: {
              value: '不早啦赶紧睡吧！好梦',
              color: common.randomHexColor(),
            },
            tiangou: {
              value: `舔狗日记: ${tiangou.content}`,
              color: common.randomHexColor(),
            },
            jitang: {
              value: `干了这碗鸡汤: ${jitang.content}`,
              color: common.randomHexColor(),
            },
          },
        }
      }
      const result = await axios({
        method: 'POST',
        url:
          'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' +
          accessToken,
        data: datas,
      })
      console.log('查看模板消息返回结果', result.data)
      res.send('推送成功')
    } catch (err) {
      console.log(err)
    }
  })()
})
//抛出这个api模块
module.exports = router
